package cn.linter.oasys.user.controller;
import cn.linter.oasys.user.entity.Flow;
import cn.linter.oasys.user.param.ApproveParam;
import cn.linter.oasys.user.service.FlowService;
import cn.linter.oasys.user.vo.OperateVO;
import com.alibaba.nacos.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import cn.linter.oasys.user.vo.OAvo;

import java.text.ParseException;
import java.util.*;

/**
 * @ClassName: FlowableController
 * @Description: 用于调用流程相关信息
 * @author: WangZe
 * @date: 2022/4/12 13:15
 */
@RestController
@RequestMapping("flowable")
@CrossOrigin(value = "http://localhost:3000")
public class FlowableController {

    @Autowired
    private FlowService flowService;

    /**
     * 发起OA
     * @param sendName
     * @param applyName
     * @return processId, taskId
     */
    @PostMapping("/sendOA")
    public OAvo sendOA(@RequestParam String sendName, @RequestParam String applyName){
        //检查参数
        if(sendName.isEmpty() || applyName.isEmpty()){
            throw new RuntimeException("参数不能为空");
        }
        //返回结果
        return flowService.sendOA(sendName,applyName);
    }

    /**
     * 审批OA
     */
    @PostMapping("/approveOA")
    public OperateVO approveOA(@RequestBody ApproveParam param){
        //检查参数
        if(param.getTaskId().isEmpty() || param.getResult().isEmpty() || param.getApplyName().isEmpty()){
            throw new RuntimeException("参数不能为空");
        }
        //返回结果
        return flowService.operateOA(param.getTaskId(),param.getApplyName(),param.getResult(),param.getText());
    }

    /**
     * 查看本人发起的所有OA
     */
    @PostMapping("/findBySendUser")
    public  List<Flow> findAllBySendUser(@RequestParam String userName){
        if(StringUtils.isBlank(userName)){
            throw new RuntimeException("参数错误");
        }
       return flowService.findBySendUser(userName);
    }

    /**
     * 查看本人发起的所有OA
     */
    @PostMapping("/findByApplyUser")
    public  List<Flow> findAllByApplyUser(@RequestParam String userName){
        if(StringUtils.isBlank(userName)){
            throw new RuntimeException("参数错误");
        }
        return flowService.findByApplyUser(userName);
    }

    /**
     * 删除flowable
     */
    @DeleteMapping("/delete")
    public int deleteByTaskId(@RequestBody Map<String,String> param){
        String taskId = param.get("taskId");
        String processId = param.get("processId");
       return flowService.delete(taskId,processId);
    }


}

